//
//  LSFTextView.swift
//  hoonpay
//
//  Created by 李胜锋 on 2017/8/8.
//  Copyright © 2017年 lishengfeng. All rights reserved.
//

import UIKit
import SnapKit

class LSFTextView: UITextView {
    
    var placeholderLable:UILabel?
    var placeholderEdgeInsets = UIEdgeInsetsMake(12, 12, 12, 12) {
        didSet {
            updatePlaceholderEdgeInsets()
        }
    }
    
    var placeholder:String? {
        set {
            placeholderLable?.text = newValue
        }
        
        get {
            return placeholderLable?.text
        }
    }
 
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setup()
//    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        updatePlaceholderEdgeInsets()
    }
    
    
    override var font: UIFont? {
        set {
            super.font = newValue
            placeholderLable?.font = newValue
        }
        get {
            return super.font
        }
    }
    
    override var text: String! {
        set {
            super.text = newValue
            textDidChangeed()
        }
        get {
            return super.text
        }
    }
    
    override var attributedText: NSAttributedString! {
        set {
            super.attributedText = newValue
            textDidChangeed()
        }
        get {
            return super.attributedText
        }
    }
    
    
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
        
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setup()
        
    }
    
    override open func awakeFromNib() {
        super.awakeFromNib()
        setup()
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    
    private func setup() {
        configPlacehodlerLabel()
        addSubview(placeholderLable!)
        updatePlaceholderEdgeInsets()
        
        NotificationCenter.default.addObserver(self, selector: #selector(LSFTextView.textDidChangeed), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
    }
    
    @objc func textDidChangeed() {
        if text.count > 0 {
            placeholderLable?.isHidden = hasText
        }
        else {
            placeholderLable?.isHidden = false
        }
    }
    
    
    
    func updatePlaceholderEdgeInsets() {
        placeholderLable?.snp.remakeConstraints { [weak self] (make) in
            make.top.equalTo(placeholderEdgeInsets.top)
            make.left.equalTo(placeholderEdgeInsets.left)
//            make.right.greaterThanOrEqualTo(edgeInsets.right)
            make.width.equalTo(self!.frame.width - placeholderEdgeInsets.left - placeholderEdgeInsets.right)
            make.bottom.greaterThanOrEqualTo(placeholderEdgeInsets.bottom)
        }
    }
    
    private func configPlacehodlerLabel() {
        placeholderLable = UILabel.init()
        placeholderLable?.font = UIFont.systemFont(ofSize: 12)
        placeholderLable?.backgroundColor = UIColor.clear
        placeholderLable?.textColor = UIColor.lsf_color16(0xc8c8c8)
        placeholderLable?.numberOfLines = 0
        placeholderLable?.textAlignment = NSTextAlignment.left
    }
}
